'''
Author: duliang thinktanker@163.com
Date: 2024-06-04 17:18:35
LastEditors: duliang thinktanker@163.com
LastEditTime: 2024-06-05 00:30:21
FilePath: 2024_6_aqdt.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''

import pyppeteer
import asyncio
import sys
import requests
from pyppeteer.network_manager import Response, Request


def pyppeteer_cookies_to_requests_cookies(pyppeteer_cookies):
    """
    将Pyppeteer的Cookie列表转换为requests.cookies.RequestsCookieJar对象
    """
    cookies_jar = requests.cookies.RequestsCookieJar()

    for cookie in pyppeteer_cookies:
        cookies_jar.set(cookie['name'], cookie['value'])
        # httpOnly=cookie['httpOnly'],
        # sameSite=cookie['sameSite'])

    return cookies_jar


# def use_cookies_in_requests(cookies, url, postjson, method='post'):
#     # 将Pyppeteer的Cookies格式转换为requests可用的格式
#     cookie_dict = dict_from_cookiejar(cookies)

#     # 使用requests.Session以维持会话状态
#     s = requests.Session()
#     s.cookies.update(cookie_dict)
#     if method == 'post':
#         s.post(url, headers=headers, data=cookie_dict, json=postjson)
#     else:
#         s.get(url, headers=headers)
#     # 发起带有Cookie的请求
#     response = s.get(url)
#     return response

# 假设你已经有了从Pyppeteer获取的cookies
# 这里直接使用上面获取的cookies作为示例
# use_cookies_in_requests(cookies, 'https://example.com/some_page')


class dati():

    def __init__(self) -> None:
        self.page_agent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Mobile Safari/537.36 Edg/125.0.0.0"
        self.headers = {'User-Agent': self.page_agent}
        self.planId = None
        self.paperId = None
        # self.cookies = None
        # self.browser = None
        # self.page = None
        # self.user_name_input = None
        # self.user_psw_input = None
        # self.login_btn = None
        # self.login_result = None

    # # 定义请求拦截处理器
    # async def handle_xhr_requests(request):
    #     if request.resourceType == 'xhr':
    #         # 这里可以根据需要处理或分析请求，例如打印请求的URL
    #         print(f"Detected XHR Request: {request.url}")
    #         # 根据需要决定是否继续请求
    #         await request.continue_()
    #     else:
    #         await request.continue_()  # 非xhr请求也继续

    async def intercept_response(self, res: Response):
        resourceType = res.request.resourceType
        if resourceType in ['xhr']:
            print(res.url)
            if "start" in res.url:
                res_json = await res.json()
                self.paperId = res_json['bean']['id']

    async def toggle_element_visibility(self, page, selector, hide=True):
        """根据hide参数切换元素的可见性：True隐藏，False显示"""
        script_hide = f'document.querySelector("{selector}").style.display = "none";'
        script_show = f'document.querySelector("{selector}").style.display = "";'

        script_to_execute = script_hide if hide else script_show
        await page.evaluate(script_to_execute)

    async def scroll_to_element(self, page, selector):
        await page.evaluate(
            f'document.querySelector("{selector}").scrollIntoView({{block: "center", behavior: "smooth"}});'
        )

    async def run(self, user_name, user_psw):
        if sys.platform == 'win32':
            userDataDir = rf"d:\pyppeteer_tmp\{user_name}_tmp"
            executablePath = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
            # executablePath = r"C:\Program Files (x86)\Microsoft\Edge Beta\Application\msedge.exe"
        else:
            userDataDir = '/home/duliang/Downloads/xx_tmp'
            executablePath = '/usr/bin/google-chrome'
        browser = await pyppeteer.launch({
            'headless':
            False,
            'args': [
                '--window-size=1024,800', '--start-maximized', '--no-sandbox',
                '--disable-infobars'
            ],
            'ignoreDefaultArgs': ['--enable-automation'],
            'dumpio':
            True,
            'userDataDir':
            userDataDir,
            'executablePath':
            executablePath
        })
        page = await browser.newPage()
        # 开启请求拦截
        # await page.setRequestInterception(True)
        # 绑定请求拦截处理器
        page.on(
            'response', lambda response: asyncio.ensure_future(
                self.intercept_response(response)))
        await page.setUserAgent(self.page_agent)
        await page.setViewport({'width': 1024, 'height': 1024})
        await page.goto(url="http://www.slaqsczsjs.com/",
                        waitUntil='networkidle2')  #networkidle2
        user_name_input = await page.J(
            'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.panel > uni-view.uni-forms > uni-form > span > uni-view:nth-child(1) > uni-view.uni-forms-item__content > uni-view.uni-easyinput > uni-view > uni-input > div > input'
        )
        user_psw_input = await page.J(
            'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.panel > uni-view.uni-forms > uni-form > span > uni-view:nth-child(2) > uni-view.uni-forms-item__content > uni-view.uni-easyinput > uni-view > uni-input > div > input'
        )
        login_btn = await page.J(
            'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.panel > uni-button'
        )
        await user_name_input.type(str(user_name))
        await user_psw_input.type(str(user_psw))
        await login_btn.click()
        # await page.screenshot({'path': 'example.png'})
        while 1:
            try:
                await page.waitForSelector(
                    'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.mask',
                    state='attached',
                    timeout=100)
                await asyncio.sleep(0.1)
            except asyncio.TimeoutError:
                break
        cookies = await page.cookies()
        cookie_jar = pyppeteer_cookies_to_requests_cookies(cookies)
        s = requests.Session()
        s.cookies.update(cookie_jar)
        me_resp = s.post('http://www.slaqsczsjs.com/exam/user/me',
                         headers=self.headers)
        if me_resp:
            me_json = me_resp.json()
            if me_json['success']:
                print(me_json['bean']['userName'])
                id_resp = s.post(
                    url='http://www.slaqsczsjs.com/exam/exam-paper/myList',
                    headers=self.headers,
                    data={
                        'page': 1,
                        'limit': 10,
                    })
                id_json = id_resp.json()
                if id_json:
                    self.planId = id_json['rows'][0]['id']
                    print(self.planId)
                    await page.goto(
                        url=
                        f'http://www.slaqsczsjs.com/#/pages/examTest/examTest?planId={self.planId}',
                        waitUntil='networkidle2')
                    while not self.paperId:
                        await asyncio.sleep(0.1)
                    answers_resp = s.post(
                        url='http://www.slaqsczsjs.com/exam/exam-paper/view',
                        headers={
                            **self.headers,
                            **{
                                'Accept-Language':
                                'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
                                'Content-type':
                                'application/x-www-form-urlencoded;charset=UTF-8',
                            }
                        },
                        data={'id': self.paperId})
                    answers_json = answers_resp.json()
                    answers_list = []
                    groups = answers_json['bean']['groups']
                    for d in groups:
                        questions = d['questions']
                        for q in questions:
                            answers_list.append(q['question']['stdAnswer'])
                    if answers_list:
                        print(answers_list)
                        sumbit_btn = await page.J(
                            'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.fixed-tail > uni-button'
                        )
                        # 隐藏元素
                        await self.toggle_element_visibility(
                            page,
                            'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.fixed-tail > uni-button',
                            hide=True)

                        for i in range(len(answers_list)):
                            temp_list = list(answers_list[i])
                            if len(temp_list) == 1:
                                print(i + 1, ' 单 ', temp_list[0])
                                s = f'#question{i+1} > uni-radio-group > uni-view:nth-child({ord(temp_list[0]) - 64}) > uni-label > uni-radio > div > div'
                                # print(s)
                                await self.scroll_to_element(page, s)
                                await page.click(s)
                                await asyncio.sleep(2)
                            else:
                                print(i + 1, ' 多')
                                for temp in temp_list:
                                    print(temp)
                                    s = f'#question{i+1} > uni-checkbox-group > uni-view:nth-child({ord(temp) - 64}) > uni-label > uni-checkbox > div > div'
                                    await self.scroll_to_element(page, s)
                                    await page.click(s)
                                    await asyncio.sleep(1)
                        # 显示元素
                        await self.toggle_element_visibility(
                            page,
                            'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.fixed-tail > uni-button',
                            hide=False)
                        await sumbit_btn.click()
                        confirm_btn = await page.J(
                            'body > uni-app > uni-modal > div.uni-modal > div.uni-modal__ft > div.uni-modal__btn.uni-modal__btn_primary'
                        )
                        await confirm_btn.click()
                        while 1:
                            try:
                                await page.waitForSelector(
                                    'body > uni-app > uni-layout > uni-content > uni-main > uni-page > uni-page-wrapper > uni-page-body > uni-view > uni-view.mask',
                                    state='attached',
                                    timeout=100)
                                await asyncio.sleep(0.1)
                            except asyncio.TimeoutError:
                                break
                        await page.waitFor(10000)

        await browser.close()

    async def execute_tasks(self, tasks):
        await asyncio.gather(*tasks)

    def start(self):
        users_lists = [
            [13141582607, 'llj123456'],
            # [15051305510, '197245jaj'],
            # [15951363060, 'llj123456'],
        ]
        # tasks = []
        # for user in users_lists:
        #     tasks.append(self.run(user_name=user[0], user_psw=user[1]))
        # asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks))
        tasks = [self.run(user[0], user[1]) for user in users_lists]
        asyncio.run(self.execute_tasks(tasks))
        # asyncio.get_event_loop().run_until_complete(
        #     self.run(username, password))


if __name__ == '__main__':
    dt = dati()
    dt.start()
